package experiment.experiment3;

/**
 * Created by lenovo on 2016/4/22.
 */
public class Complextest {

    public static void main (String args[])
    {
        ComplexNumber a = new ComplexNumber(20,10);
        ComplexNumber b = new ComplexNumber(5,5);
        ComplexNumber x = new ComplexNumber();
        double z = 10.0;

        x = a.complexAdd(b);
        if(x.getRealPart() !=25.0 )
            System.out.println("test failed 1!");
        x = a.complexAdd(z);
        if(x.getRealPart() !=30.0 )
            System.out.println("test failed 11!");
        x = a.complexAdd(b);
        if( x.getImaginaryPart()!=15.0 )
            System.out.println("test failed 2!");
        x = a.complexAdd(z);
        if( x.getImaginaryPart()!=10.0 )
            System.out.println("test failed 22!");
        x = a.complexMinus(b);
        if( x.getRealPart()!=15.0 )
            System.out.println("test failed 3!");
        x = a.complexMinus(z);
        if( x.getRealPart()!=10.0 )
            System.out.println("test failed 33!");
        x = a.complexMinus(b);
        if( x.getImaginaryPart()!=5.0 )
            System.out.println("test failed 4!");
        x = a.complexMinus(z);
        if( x.getImaginaryPart()!=10.0 )
            System.out.println("test failed 44!");
        x = a.complexMulti(b);
        if( x.getRealPart() !=50.0 )
            System.out.println(x.getRealPart()+"test failed 5!");
        x = a.complexMulti(z);
        if( x.getRealPart() !=200.0 )
            System.out.println("test failed 55!");
        x = a.complexMulti(b);
        if( x.getImaginaryPart()!=150.0 )
            System.out.println("test failed 6!");
        x = a.complexMulti(z);
        if( x.getImaginaryPart()!=100.0 )
            System.out.println("test failed 66!");

        x.setRealPart(1);
        if(x.getRealPart()!=1)
            System.out.println("test7 fail");
        x.setImaginaryPart(-1);
        if (x.getImaginaryPart()!=-1)
            System.out.println("test8 fail");

        else
            System.out.println("test passed!");
    }
}

class ComplexNumber
{
    private double realpart;
    private double imaginpart;
    ComplexNumber()
    {
        realpart = 0.0;
        imaginpart = 0.0;
    }
    ComplexNumber(double r,double I)
    {
        realpart = r;
        imaginpart = I;
    }

    double getRealPart()
    {
        return realpart;
    }
    double getImaginaryPart()
    {
        return  imaginpart;
    }
    void setRealPart(double d)
    {
        realpart = d;
    }
    void setImaginaryPart(double d)
    {
        imaginpart = d;
    }
    ComplexNumber complexAdd(ComplexNumber c)
    {
        return new ComplexNumber( this.realpart + c.getRealPart(), this.imaginpart + c.getImaginaryPart());
    }
    ComplexNumber complexAdd(double c)
    {
        return new ComplexNumber( this.realpart + c, this.imaginpart);
    }

    ComplexNumber complexMinus(ComplexNumber c)
    {
        return new ComplexNumber( this.realpart - c.getRealPart(),this.imaginpart - c.getImaginaryPart());
    }
    ComplexNumber complexMinus(double c)
    {
        return new ComplexNumber( this.realpart - c, this.imaginpart);
    }

    ComplexNumber complexMulti(ComplexNumber c)
    {
        return new ComplexNumber( this.realpart * c.getRealPart() - this.imaginpart * c.getImaginaryPart(),
                this.realpart * c.getImaginaryPart() + this.imaginpart * c.getRealPart());
    }
    ComplexNumber complexMulti(double c)
    {
        return new ComplexNumber( this.realpart * c, this.imaginpart * c );
    }

    public String toString()
    {
        return "(" + realpart + " + " + imaginpart + " i" + ")";
    }
}